package com.example.todo;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Build;

public class EditTaskActivity extends Activity {
	DBAdapter myDb;
	public  static long TASK_ID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Don't allow the keyboard to pup up when the page loads.
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
		setContentView(R.layout.fragment_edit_task);
		Intent intent = getIntent();

		// Set original text
		String message = intent.getStringExtra(TaskDetailsActivity.EXTRA_TASK_NAME);
		TextView editText = (TextView) findViewById(R.id.task_name);
		editText.setText(message);
		
		// Next release, update the spinner when editing a task
		//String strCategory = intent.getStringExtra(TaskDetailsActivity.EXTRA_CATEGORY);
		//TextView editTextCategory = (TextView) findViewById(R.id.add_category);
		//editTextCategory.setText(strCategory);
		
		String strSubject = intent.getStringExtra(TaskDetailsActivity.EXTRA_SUBJECT);
		TextView editTextSubject = (TextView) findViewById(R.id.new_subject);
		editTextSubject.setText(strSubject);
		
		String strProject = intent.getStringExtra(TaskDetailsActivity.EXTRA_PROJECT);
		TextView editTextProject  = (TextView) findViewById(R.id.new_project );
		editTextProject.setText(strProject);
		
		String strDate = intent.getStringExtra(TaskDetailsActivity.EXTRA_DATE);
		TextView editTextDate  = (TextView) findViewById(R.id.new_date );
		editTextDate.setText(strDate);
		
		String strLocation = intent.getStringExtra(TaskDetailsActivity.EXTRA_LOCATION);
		TextView editTextLocation  = (TextView) findViewById(R.id.new_location );
		editTextLocation.setText(strLocation);
		
		String strDescription = intent.getStringExtra(TaskDetailsActivity.EXTRA_DESCRIPTION);
		TextView editTextDescription  = (TextView) findViewById(R.id.new_description);
		editTextDescription.setText(strDescription);
				
		String strID = intent.getStringExtra(TaskDetailsActivity.EXTRA_ID);		
		TASK_ID = Long.parseLong(strID);
	}

	// START NAVIGATION MENU CODE
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_task, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_edit_task,
					container, false);
			return rootView;
		}
	}
	// END NAVIGATION MENU CODE
	
	private void openDB() {
		myDb = new DBAdapter(this);
		myDb.open();
	}
	private void closeDB() {
		myDb.close();
	}
	
	//Close this activity and go to the main page.
	public void onClickClose(View view){
    	Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
	}
	
	// Save changes to database
	public void onClick_SaveTask(View view){
		openDB();
		EditText newTask = (EditText) findViewById(R.id.task_name);
		String stringNewTask = newTask.getText().toString();
		Spinner newCategory = (Spinner) findViewById(R.id.add_category);
		String stringNewCategory = newCategory.getSelectedItem().toString();
		EditText newSubject = (EditText) findViewById(R.id.new_subject);
		String stringSubject = newSubject.getText().toString();
		EditText newProject = (EditText) findViewById(R.id.new_project);
		String stringProject = newProject.getText().toString();
		EditText newDate = (EditText) findViewById(R.id.new_date);
		String stringDate = newDate.getText().toString();
		EditText newLocation = (EditText) findViewById(R.id.new_location);
		String stringLocation = newLocation.getText().toString();
		EditText newDescription = (EditText) findViewById(R.id.new_description);
		String stringDescription = newDescription.getText().toString();
		
		//Add the input into the database
		long thisID = TASK_ID;
    	myDb.updateRow(thisID, stringNewTask, stringNewCategory, stringSubject, stringProject, stringDate,
    			stringDescription, stringLocation);
    	//close the DB
    	closeDB();
    	//Close this activity and go to the main page.
    	Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
    }

}
